package com.asiainfo.heshanghui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asiainfo.heshanghui.adapter.BiuooCommonAdapter;
import com.asiainfo.heshanghui.adapter.BiuooViewHolder;
import com.framework2.customview.CustomListView;
import com.framework2.customview.TitleView;
import com.framework.Utils.Utils;
import com.framework.Utils.Y;
import com.framework2.utils.BiuooCollectionUtil;
import com.framework2.utils.BiuooCustomProgressDialogUtils;
import com.asiainfo.heshanghui.R;
import com.asiainfo.heshanghui.adapter.BiuooListItem;
import com.framework2.base.BiuooBaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 订购清单
 */
public class BiuooSaleActivity extends BiuooBaseActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    private TitleView titleView;
    private TextView tv_accessories_money, tv_model_money;
    private TextView tv_commit;
    private LinearLayout phoneLayout, accessoriesLayout, modelLayout;
    private LinearLayout earningLayout;//本店商品小计金额
    private CustomListView customLv1;
    private CustomListView customLv2;
    private CustomListView customLv3;
    private BiuooCommonAdapter<BiuooListItem> adapter1;
    private BiuooCommonAdapter<BiuooListItem> adapter2;
    private BiuooCommonAdapter<BiuooListItem> adapter3;
    private List<BiuooListItem> list1 = new ArrayList<>();//手机的数据
    private List<BiuooListItem> list2 = new ArrayList<>();//配件的数据
    private List<BiuooListItem> list3 = new ArrayList<>();//流量的数据
    private boolean lastChecked1;//上个页面是否checked
    private boolean lastChecked2;//上个页面是否checked
    private boolean lastChecked3;//上个页面是否checked
    private List<BiuooListItem> _list1;
    private List<BiuooListItem> _list2;
    private List<BiuooListItem> _list3;
    private double accessories_money, _model_money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setTranslucentStatus(this, true);
        Utils.setStatusBarTintDrawable(this, R.mipmap.biuoo_bg_top);
        setContentView(R.layout.biuoo_activity_salelist);
        initIntent();
        initView();
        tv_commit.setVisibility(View.GONE);
        tv_accessories_money.setVisibility(View.GONE);
        tv_model_money.setVisibility(View.GONE);
        BiuooCustomProgressDialogUtils.showProcessDialog(this, "正在请求");

        Utils.h.postDelayed(new Runnable() {
            @Override
            public void run() {
                BiuooCustomProgressDialogUtils.dismissProcessDialog();
                tv_commit.setVisibility(View.VISIBLE);
                tv_accessories_money.setVisibility(View.VISIBLE);
                tv_model_money.setVisibility(View.VISIBLE);
                initList1();
                initList2();
                initList3();
            }
        }, 1000);
    }

    private void initIntent() {
        if (getIntent() != null) {
            _list1 = (List<BiuooListItem>) getIntent().getSerializableExtra("list1");
            _list2 = (List<BiuooListItem>) getIntent().getSerializableExtra("list2");
            _list3 = (List<BiuooListItem>) getIntent().getSerializableExtra("list3");
            Y.y("_list1:" + (_list1 == null));
            Y.y("_list2:" + (_list2 == null));
            Y.y("_list3:" + (_list3 == null));
            if (!BiuooCollectionUtil.getInstance().isEmpty(_list1)) {
                for (int i = 0; i < _list1.size(); i++) {
                    BiuooListItem biuooListItem = _list1.get(i);
                    if (biuooListItem != null && biuooListItem.isChecked()) {
                        lastChecked1 = true;
                        accessories_money += biuooListItem.get_price();
                    }
                }
            }
            if (!BiuooCollectionUtil.getInstance().isEmpty(_list2)) {
                for (int i = 0; i < _list2.size(); i++) {
                    BiuooListItem biuooListItem = _list2.get(i);
                    if (biuooListItem != null && biuooListItem.isChecked()) {
                        lastChecked2 = true;
                        accessories_money += biuooListItem.get_price();
                    }
                }
            }
            if (!BiuooCollectionUtil.getInstance().isEmpty(_list3)) {
                for (int i = 0; i < _list3.size(); i++) {
                    BiuooListItem biuooListItem = _list3.get(i);
                    if (biuooListItem != null && biuooListItem.isChecked()) {
                        lastChecked3 = true;
                        _model_money += biuooListItem.get_price();
                    }
                }
            }
        }
    }

    private void initList1() {
//        BiuooListItem item = new BiuooListItem();
//        item.setTitle("外置柔光");
//        item.setPrice("¥100.00");
//        item.setCount("X12");
//        BiuooListItem item2 = new BiuooListItem();
//        item2.setTitle("外置柔光22");
//        item2.setPrice("¥102.00");
//        item2.setCount("X20");
//        list1.add(item);
//        list1.add(item2);
        if (!BiuooCollectionUtil.getInstance().isEmpty(_list1)) {
            for (int i = 0; i < _list1.size(); i++) {
                if (_list1.get(i).isChecked()) {
                    list1.add(_list1.get(i));
                }
            }
            if (!BiuooCollectionUtil.getInstance().isEmpty(list1)) {
                adapter1 = initAdapter(adapter1, 0);
                customLv1.setAdapter(adapter1);
            }
        }
    }

    private void initList2() {
//        BiuooListItem item = new BiuooListItem();
//        item.setTitle("外置柔光qw");
//        item.setPrice("¥110.00");
//        item.setCount("X2");
//        BiuooListItem item2 = new BiuooListItem();
//        item2.setTitle("外置柔光qw");
//        item2.setPrice("¥110.00");
//        item2.setCount("X2");
//        list2.add(item);
//        list2.add(item2);
        if (!BiuooCollectionUtil.getInstance().isEmpty(_list2)) {
            for (int i = 0; i < _list2.size(); i++) {
                if (_list2.get(i).isChecked()) {
                    list2.add(_list2.get(i));
                }
            }
            if (!BiuooCollectionUtil.getInstance().isEmpty(list2)) {
                adapter2 = initAdapter(adapter2, 1);
                customLv2.setAdapter(adapter2);
            }
        }
    }

    private void initList3() {
//        BiuooListItem item = new BiuooListItem();
//        item.setTitle("流量套餐");
//        item.setPrice("¥100.00");
//        item.setShowIcon(false);
//        item.setCount("X1");
//        BiuooListItem item2 = new BiuooListItem();
//        item2.setTitle("流量套餐22");
//        item2.setPrice("¥102.00");
//        item2.setCount("X2");
//        item2.setShowIcon(true);
//        list3.add(item);
//        list3.add(item2);
        if (!BiuooCollectionUtil.getInstance().isEmpty(_list3)) {
            for (int i = 0; i < _list3.size(); i++) {
                if (_list3.get(i).isChecked()) {
                    list3.add(_list3.get(i));
                }
            }
            if (!BiuooCollectionUtil.getInstance().isEmpty(list3)) {
                adapter3 = initAdapter(adapter3, 2);
                customLv3.setAdapter(adapter3);
            }
        }
    }

    /**
     * @param type 0--list1,1--list1,2--list3；
     * @return
     */
    private BiuooCommonAdapter<BiuooListItem> initAdapter(BiuooCommonAdapter<BiuooListItem> adapter, final int type) {
        if (adapter == null) {
            adapter = new BiuooCommonAdapter<BiuooListItem>(BiuooSaleActivity.this, type == 0 ? list1 : (type == 1 ? list2 : list3), R.layout.biuoo_listitem_sale) {
                @Override
                public View getListItemview(BiuooViewHolder viewHolder, View view, final BiuooListItem item, int position, ViewGroup parent) {
                    viewHolder.setImageResource(R.id.iv_icon, type == 2 ? R.mipmap.biuoo_icon_guangdong : item.getImgRes());
                    viewHolder.setText(R.id.tv_title, item.getTitle());
                    viewHolder.setText(R.id.tv_price, "¥"+item.get_price());
                    if (type == 2) {
                        viewHolder.setText(R.id.tv_count, item.getCount());
                    } else {
                        viewHolder.setText(R.id.tv_count, "X1");
                    }
                    return null;
                }
            };
        }
        return adapter;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_commit:
                BiuooCustomProgressDialogUtils.showProcessDialog(this, "生成订单");
                Utils.h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        BiuooCustomProgressDialogUtils.dismissProcessDialog();
                        Intent intent = new Intent(BiuooSaleActivity.this, BiuooSaleSuccessActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        overridePendingTransition(R.anim.biuoo_slide_right_in,
                                R.anim.biuoo_slide_left_out);
                    }
                }, 1000);
                break;
            default:
                break;
        }
    }


    private void initView() {
        titleView = (TitleView) findViewById(R.id.titleView);
        titleView.setTitle("订购清单").setRightVisible(false);
        customLv1 = (CustomListView) findViewById(R.id.customLv1);
        customLv2 = (CustomListView) findViewById(R.id.customLv2);
        customLv3 = (CustomListView) findViewById(R.id.customLv3);
        tv_accessories_money = (TextView) findViewById(R.id.tv_accessories_money);
        tv_model_money = (TextView) findViewById(R.id.tv_model_money);

        tv_commit = (TextView) findViewById(R.id.tv_commit);

        phoneLayout = (LinearLayout) findViewById(R.id.phoneLayout);
        accessoriesLayout = (LinearLayout) findViewById(R.id.accessoriesLayout);
        modelLayout = (LinearLayout) findViewById(R.id.modelLayout);
        earningLayout = (LinearLayout) findViewById(R.id.earningLayout);
        if (!lastChecked1) {
            phoneLayout.setVisibility(View.GONE);
        }
        if (!lastChecked2) {
            accessoriesLayout.setVisibility(View.GONE);
        }

        if(lastChecked1||lastChecked2){
            earningLayout.setVisibility(View.VISIBLE);
            tv_accessories_money.setText("¥" + accessories_money);
        }else {
            earningLayout.setVisibility(View.GONE);
        }
        if (!lastChecked3) {
            modelLayout.setVisibility(View.GONE);
        } else {
            tv_model_money.setText("¥" + _model_money);
        }

    }

}
